﻿using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace MeiTool.Common
{  
    public class SpireDocBookmark
    {
        private Document? doc = null;

        public SpireDocBookmark(Document document)
        {
            doc = document;
        }

        /// <summary>
        /// 用文本替换指定书签的内容
        /// </summary>
        /// <param name="bookmarkName">书签名</param>
        /// <param name="text">文本</param>
        /// <param name="saveFormatting">删除原始书签内容时，是否保留格式</param>
        /// <returns>TextRange</returns>
        public TextRange? ReplaceContent(string bookmarkName, string text, bool saveFormatting)
        {
            BookmarksNavigator navigator = new BookmarksNavigator(doc);
            navigator.MoveToBookmark(bookmarkName);//指向特定书签
            navigator.DeleteBookmarkContent(saveFormatting);//删除原有书签内容     
            Spire.Doc.Interface.ITextRange textRange = navigator.InsertText(text);//写入文本
            return textRange as TextRange;
        }

        /// <summary>
        /// 用图片替换指定书签的内容
        /// </summary>
        /// <param name="bookmarkName">书签名</param>
        /// <param name="picPath">图片路径</param>
        /// <param name="widthScale">宽度缩放比例，0以上正整数</param>
        /// <param name="heightScale">高度缩放比例，0以上正整数</param>
        /// <param name="wrapStyle">环绕方式</param>
        /// <param name="horizontalAlignment"></param>
        public void ReplaceContent(string bookmarkName, string picPath, float widthScale, float heightScale, TextWrappingStyle wrapStyle, ShapeHorizontalAlignment horizontalAlignment)
        {
            //创建BookmarksNavigator实例
            BookmarksNavigator bn = new BookmarksNavigator(doc);
            //找到名为Spire的书签
            bn.MoveToBookmark(bookmarkName, true, true);
            //添加一个secton并命名为section0
            Section section0 = doc.AddSection();
            //为section0添加一个段落
            Paragraph paragraph = section0.AddParagraph();
            paragraph.Text = "";
            //加载一张图片
            Image image = Image.FromFile(picPath);
            //为段落添加图片
            DocPicture picture = paragraph.AppendPicture(image);
            picture.HorizontalPosition = 0f;
            picture.VerticalPosition = 0f; //设置图像的停靠位置方式
            //设置图片大小          
            picture.Width = 300;
            picture.Height = 300;
            picture.HorizontalAlignment = ShapeHorizontalAlignment.Center;
            picture.TextWrappingStyle = TextWrappingStyle.None;

            //把含有图片的段落插入到书签位置
            bn.InsertParagraph(paragraph);
            doc.Sections.Remove(section0);

        }

        /// <summary>
        /// 用表格替换指定书签的内容
        /// </summary>
        /// <param name="bookmarkName">书签名</param>
        /// <param name="datatable">表格</param>
        /// <param name="columnWidth">列宽 默认200</param>
        /// <param name="horizontalAlignment">对齐方式 默认左对齐</param>
        /// <param name="cellWidthType">列宽宽度调整类型 默认自动调整</param>
        public void ReplaceContent(string bookmarkName, DataTable datatable, float columnWidth = 200f, CellWidthType cellWidthType = CellWidthType.Auto, RowAlignment horizontalAlignment = RowAlignment.Left)
        {
            Table table = new Table(doc, true, 1f);//初始化Table对象
            table.ResetCells(datatable.Rows.Count, datatable.Columns.Count);//设置行数和列数
            //填充数据
            for (int i = 0; i < datatable.Rows.Count; i++)
            {
                for (int j = 0; j < datatable.Columns.Count; j++)
                {
                    table.Rows[i].Cells[j].AddParagraph().AppendText(datatable.Rows[i][j].ToString());
                }
            }
            //设置列宽
            for (int i = 0; i < datatable.Rows.Count; i++)
            {
                for (int j = 0; j < datatable.Columns.Count; j++)
                { 
                    table.Rows[i].Cells[j].SetCellWidth(columnWidth, cellWidthType); 
                }
            }
            table.TableFormat.HorizontalAlignment = horizontalAlignment;//表格水平对齐方式
            BookmarksNavigator navigator = new BookmarksNavigator(doc);
            navigator.MoveToBookmark(bookmarkName);
            TextBodyPart body = new TextBodyPart(doc);
            body.BodyItems.Add(table);
            navigator.ReplaceBookmarkContent(body);
        }
    }
}
